package com.vma.jifu.backsys.controller;

import com.jfinal.core.Controller;

import com.jfinal.plugin.activerecord.Page;
import com.vma.jifu.bizModel.model.CashOut;
import com.vma.jifu.bizModel.model.OrderList;
import com.vma.jifu.bizModel.model.User;
import com.vma.jifu.bizModel.model.UserCount;
import com.vma.jifu.service.UserService;
import com.vma.jifu.service.impl.UserServiceImpl;
import com.vma.tools.ReturnsUtil;
import org.apache.velocity.tools.generic.DateTool;

import javax.jws.soap.SOAPBinding;
import java.util.List;

/**
 * Created by chenzui on 2017/4/11.
 */
public class UserController extends BaseController {


    UserService userService=new UserServiceImpl();

    public void qryUserList(){
        try{
            Integer cPage = getParaToInt("cPage");
            Integer pSize = getParaToInt("pSize");
            String  status = getPara("status");
            String phone = getPara("phone");
            String start = getPara("start");
            String end = getPara("end");

            Page<User> user = userService.qryUserList(cPage, pSize, status, phone, start, end);

            List<User> userlist= user.getList();
            int tatol = user.getTotalRow();


            renderJson(ReturnForm(userlist,tatol));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }

    }
//
//    /**
//     * 重置密码
//     */
//    public void resetPwd(){
//        try{
//            Integer userId = getParaToInt("userId");
//            UserService.resetPwd(userId);
//            renderJson(ReturnsUtil.returnSuccess("成功"));
//        }catch (Exception e){
//            e.printStackTrace();
//            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
//        }
//    }
//
//    /**
//     * 用户详情，包含订单信息等
//     */
//    public void userDetail(){
//        try{
//            Integer userId = getParaToInt("userId");
//            User user = UserService.userDetail(userId);
//            setAttr("user",user);
//            setAttr("date", new DateTool());
//            // TODO: 2017/4/25 把时间格式化加入全局拦截器
////            List<OrderList> orders =  OrderService.qryOrderList("","",1,100,"","",userId).getList();
////            setAttr("orders",orders);
//            renderVelocity("/shop/user/user_detail.html");
//        }catch (Exception e){
//            e.printStackTrace();
//            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
//        }
//    }
//
    public void updateStatus(){

            Integer userId = getParaToInt("id");
            userService.updateStatus(userId);
            renderJson(ReturnsUtil.returnSuccess("成功"));

    }

    public void getOrderDetail(){
        String id = getPara("id");

        User user = User.dao.findById(id);

        List<User> child = User.dao.find("select * from user where parend_user_account ="+id);


        List<OrderList> orderLists= OrderList.dao.find("select * from order_list where user_id="+id);

        UserCount userCount = UserCount.dao.findById(id);

        if(userCount != null){
            float order_price_num = userCount.getOrderPriceNum()/100;
            float return_price_num = userCount.getReturnPriceNum()/100;
            float sale_price_num = userCount.getSalePriceNum()/100;

            userCount.set("order_price_num",order_price_num);

            userCount.set("return_price_num",return_price_num);

            userCount.set("sale_price_num",sale_price_num);

            setAttr("userCount",userCount);
        }

        List<CashOut> cashOuts= CashOut.dao.find(" select * from cash_out where  status= 1 and user_id ="+id);
        float total = 0;
        if(cashOuts.size()>0){
            for(CashOut temp : cashOuts){
                total += temp.getCashNum()/100;
            }

        }
        if(user.getFirstParentUserId() != null){
            User parent = User.dao.findById(user.getFirstParentUserId());
            setAttr("parent",parent);
        }


        setAttr("cashOuts",cashOuts);
        setAttr("child",child.size());
        setAttr("user",user);
        setAttr("cashAll",total);



        renderVelocity("/newshop/user/userDetail.html");
    }
}
